package LeetCodeHot100TopInterview;

import java.util.HashSet;
import java.util.Set;

public class Q003_NoRepeatMax {
    //同剑指offer Q48
    public int lengthOfLongestSubstring(String s) {
        char[] str = s.toCharArray();
        Set<Character> set = new HashSet<>();
        int left = 0;
        int right = 0;
        int max = 0;
        while (left != str.length && right != str.length){
            if (!set.contains(str[right])){
                set.add(str[right]);
                //结算
                right++;
                max = Math.max(max, right - left);

            }else {
                //重复了
                //跳到重复位置的下一跳
                for (int i = left; i < right; i++) {
                    if (str[i] == str[right]){
                        left = i + 1;
                        //还要让 right 再结算一遍，所以抵消上面的 right++
//                        right--;
                        set.remove(str[right]);
                        break;
                    }
                    set.remove(str[i]);
                }
            }
        }
        return max;
    }
}
